package com.go.smsautosender;


import java.util.Date;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.telephony.gsm.SmsManager;
import android.util.Log;

public class VerifyUser {
//	private int[] AUTH_KEY = {0, 1, 3, 8};
	private int[] AUTH_KEY = {0, 1, 3, 8, 2, 5};
	private String AUTH_ADMIN = "01033886876";
	
	private Context mContext;
	
	public VerifyUser(Context context) {
		mContext = context;
	}
	
	public boolean verify() {
		
		if(isFirstExecute()) {
			authToSMS();
		} else {
//			if(checkUseCount()) {
//				return true;
//			}
			return true;
		}
		
		return false;
	}
	
	private boolean isFirstExecute() {
		boolean isFirst = true;
		SharedPreferences pref = mContext.getSharedPreferences("TU", Context.MODE_PRIVATE);
		isFirst = pref.getBoolean("isFirst", true); 
		
		return isFirst;
	}
	
	private boolean checkUseCount() {
		SharedPreferences pref = mContext.getSharedPreferences("TU", Context.MODE_PRIVATE);
		int usecount = pref.getInt("count", 10);
		
		if(usecount > 10) {
			return false;
		} else {
			SharedPreferences.Editor edit = pref.edit();
			edit.putInt("count", ++usecount);
			edit.commit();
			return true;
		}
	}
	
	private void authToSMS() {
		String authCode = "";
		
		authCode = getAuthCode();
		sendSMS(authCode);
		
		createAuthDialog(authCode);
	}
	
	
	private String getAuthCode() {
		  StringBuffer authCode = null;
		  String authBuf = null, randBuf = null;
		  
		  randBuf = Double.toString(Math.random());
		  randBuf = randBuf.substring(15);
		  while(randBuf.startsWith("0")) {
		   randBuf = randBuf.substring(1);
		  }
		  
		  while(authBuf == null) {
		   try {
		    authBuf = String.valueOf(Long.valueOf(randBuf) * new Date().getTime());
		   } catch(Exception e) { }
		  }
		  
		  if(authBuf.length() < AUTH_KEY[3]) {
		   authBuf += "12782238";
		  }
		  
		  authCode = new StringBuffer();
		  authCode.append(authBuf.charAt(AUTH_KEY[0]));
		  authCode.append(authBuf.charAt(AUTH_KEY[1]));
		  authCode.append(authBuf.charAt(AUTH_KEY[2]));
		  authCode.append(authBuf.charAt(AUTH_KEY[3]));
		  authCode.append(authBuf.charAt(AUTH_KEY[4]));
		  authCode.append(authBuf.charAt(AUTH_KEY[5]));
		  
		  return authCode.toString();
	}
	
	private void sendSMS(String authCode) {
		PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, new Intent("SMS_SENT"), 0);
		PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0, new Intent("SMS_DELIVERED"), 0);
		
//		mContext.registerReceiver(new BroadcastReceiver() {
//
//			@Override
//			public void onReceive(Context arg0, Intent arg1) {
//				// TODO Auto-generated method stub
//				switch(getResultCode()) {
//				case Activity.RESULT_OK:
//					
//					break;
//				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
//					Log.e("VerifyUser", "SMS error : [RESULT_ERROR_GENERIC_FAILURE]");
//					break;
//				case SmsManager.RESULT_ERROR_NO_SERVICE:
//					Log.e("VerifyUser", "SMS error : [RESULT_ERROR_NO_SERVICE]");
//					break;
//				case SmsManager.RESULT_ERROR_NULL_PDU:
//					Log.e("VerifyUser", "SMS error : [RESULT_ERROR_NULL_PDU]");
//					break;
//				case SmsManager.RESULT_ERROR_RADIO_OFF:
//					Log.e("VerifyUser", "SMS error : [RESULT_ERROR_RADIO_OFF]");
//					break;
//				}
//			}
//		}, new IntentFilter("SMS_SENT"));
		
		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(AUTH_ADMIN, null, "[" + authCode + "]", null, null);
	}
	
	public void createAuthDialog(String authCode) {
		mContext.startActivity(new Intent(mContext, VerifyDialog.class).putExtra("authcode", authCode));
	}
}
